package com.data4.supplier.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.data4.feignapi.common.constants.BrandConstants;
import com.data4.feignapi.dto.BrandDTO;
import com.data4.feignapi.dto.PageDTO;
import com.data4.feignapi.dto.Result;
import com.data4.feignapi.entity.Brand;
import com.data4.supplier.mapper.BrandMapper;
import com.data4.supplier.service.BrandService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Forever
 * @since 2024-12-11
 */
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {

    @Override
    public Result addBrand(BrandDTO brandDTO, Integer supplierId) {
        Brand brand = BeanUtil.toBean(brandDTO, Brand.class);
        brand.setSupplierId(supplierId);
        save(brand);
        return Result.ok(BrandConstants.ADD_SUCCESS);
    }

    @Override
    public Result updateBrand(BrandDTO brandDTO, Integer brandId, Integer supplierId) {
        if (BeanUtil.isEmpty(brandDTO)) return Result.fail(BrandConstants.UPDATE_NOT_ARG_FAIL);
        if (!exists(new LambdaQueryWrapper<Brand>()
                .eq(Brand::getBrandId, brandId))) return Result
                .fail(BrandConstants.UPDATE_FAIL);
        Brand brand = BeanUtil.toBean(brandDTO, Brand.class);
        update(brand, new LambdaQueryWrapper<Brand>()
                .eq(Brand::getSupplierId, supplierId)
                .eq(Brand::getBrandId, brandId));
        return Result.ok(BrandConstants.UPDATE_SUCCESS);

    }


    @Override
    public Result getBrandByTime(Integer current, Integer num, Integer supplierId) {
        Page<Brand> brandPage = lambdaQuery()
                .eq(Brand::getSupplierId, supplierId)
                .page(new Page<>(current, num));
        List<Brand> records = brandPage
                .getRecords();
        PageDTO pageDTO = new PageDTO();
        pageDTO.setList(records);
        pageDTO.setTotalNum(brandPage.getTotal());
        return Result.ok(pageDTO);
    }

    @Override
    public Result getBrandByNameLike(String brandName, Integer current,
                                     Integer num, Integer supplierId) {
        Page<Brand> brandPage = lambdaQuery()
                .eq(Brand::getSupplierId, supplierId)
                .like(Brand::getBrandName, brandName)
                .page(new Page<>(current, num));
        List<Brand> records = brandPage
                .getRecords();
        PageDTO pageDTO = new PageDTO();
        pageDTO.setList(records);
        pageDTO.setTotalNum(brandPage.getTotal());
        return Result.ok(pageDTO);
    }

    @Override
    public Boolean removeBrands(List<Long> brandIdList, int parseInt) {
        boolean remove = remove(new LambdaQueryWrapper<Brand>()
                .eq(Brand::getSupplierId, parseInt)
                .in(Brand::getBrandId, brandIdList));
        return remove;
    }
}
